package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@Api("通用api")
@Slf4j
@RestController
@RequestMapping("admin/common")
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    //请求中要携带上需要上传的文件
    public Result<String> saveOss(MultipartFile file) {
        log.info("file {}",file);
        try {
            //  获取原始的文件名
            String originalFilename = file.getOriginalFilename();
            //在oss中存储名字就是UUID + 文件的后缀名
            String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            String resultURL = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(resultURL);
        } catch (IOException e) {
            log.error(" 错误 {}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

}



